<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="名称">
          <el-input
            v-model="queryParams.keywords"
            placeholder="输入搜索关键词"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >

          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <template #header>
        <!-- <el-button type="success" @click="openDialog()"
          ><i-ep-plus />新增</el-button
        > -->
      </template>
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :stripe="true"
        size="large"
        max-height="600px"
      >
        <el-table-column align="center" label="序号" type="index" width="100" />

        <el-table-column
          align="center"
          width="120"
          label="预约用户"
          prop="bookUser"
        />
        <el-table-column
          align="center"
          width="120"
          label="实验室"
          prop="labName"
        />
        <el-table-column
          align="center"
          width="120"
          label="座位编号"
          prop="seatNo"
        />

        <el-table-column
          align="center"
          width="200"
          label="开始时间"
          prop="bookStartTime"
        />
        <el-table-column
          align="center"
          width="200"
          label="结束时间"
          prop="bookEndTime"
        />

        <el-table-column
          align="center"
          width="200"
          label="驳回原因"
          prop="reason"
        />
        <el-table-column
          align="center"
          label="实际结束时间"
          width="200"
          prop="realEndTime"
        />
        <el-table-column align="center" label="状态" width="120" prop="status">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
            <el-tag v-if="scope.row.status === 1" type="danger">驳回</el-tag>
            <el-tag v-if="scope.row.status === 2" type="success">通过</el-tag>
            <el-tag v-if="scope.row.status === 3">完成</el-tag>
            <el-tag v-if="scope.row.status === 4" type="info">取消</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 预约记录表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="30%"
      @close="closeDialog"
    >
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="所属社区" prop="comId">
          <el-tree-select
            v-model="formData.comId"
            placeholder="请选择所属社区"
            :data="communityList"
            filterable
            check-strictly
            :render-after-expand="false"
          />
        </el-form-item>

        <el-form-item label="标题" prop="name">
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>

        <el-form-item label="是否展示" prop="month">
          <el-radio-group v-model="formData.priority">
            <el-radio :label="0">展示</el-radio>
            <el-radio :label="1">不展示</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="预约记录内容" prop="remark">
          <el-input
            v-model="formData.content"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {
  getPageBookRecordAPI,
  getFormBookRecordAPI,
  addBookRecordAPI,
  updateBookRecordAPI,
  deleteBookRecordAPI,
} from "@/api/bookRecord/index";

import { IPage, IBookRecord } from "@/api/bookRecord/types";

import { ref, reactive } from "vue";
/* import { getCountryBaseListBookRecordAPI } from "@/api/countryBase/index";
import { CountryBase } from "@/api/countryBase/types"; */

const total = ref(0);
const loading = ref(false);
const tableData = ref<IBookRecord[]>([]);
const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);
const communityList = ref<any[]>([]);
// 表单数据
const formData = reactive<IBookRecord>({});
// 表单规则
const rules = reactive({
  title: [{ required: true, message: "请输入", trigger: "blur" }],
});

const dialog = reactive<DialogOption>({
  visible: false,
});
const queryParams = reactive<IPage>({
  pageNum: 1,
  pageSize: 10,
});

onMounted(() => {
  handleQuery();
});

// 获取预约记录列表
function handleQuery() {
  loading.value = true;
  getPageBookRecordAPI(queryParams)
    .then(({ data }) => {
      tableData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */

function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

// /** 打开预约记录表单弹窗 */
// function openDialog(id?: number) {
//   if (id) {
//     dialog.title = "修改预约记录";
//     getFormBookRecordAPI(id).then(({ data }) => {
//       Object.assign(formData, data);
//     });
//   } else {
//     dialog.title = "新增预约记录";
//   }
//   dialog.visible = true;
// }

/** 预约记录保存提交 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        updateBookRecordAPI(id, formData)
          .then(() => {
            ElMessage.success("修改成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        addBookRecordAPI(formData)
          .then(() => {
            ElMessage.success("新增成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 重置表单 */
function resetForm() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}
/** 删预约记录 */
function handleDelete(id: number) {
  loading.value = true;
  deleteBookRecordAPI(id)
    .then(() => {
      ElMessage.success("删除成功");
      resetQuery();
    })
    .finally(() => (loading.value = false));
  resetForm();
}
</script>

<style lang="scss" scoped></style>
